package es.uned.ii.si3.web.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import es.uned.ii.si3.business.GestionSolicitudes;
import es.uned.ii.si3.exceptions.ObjectNotFoundException;
import es.uned.ii.si3.model.Solicitud;
import es.uned.ii.si3.util.UserAgentDetector;
import es.uned.ii.si3.web.actions.generic.LoginRequiredDispatchAction;
import es.uned.ii.si3.web.forms.ListarSolicitudesForm;

public class AceptarSolicitudesAction extends LoginRequiredDispatchAction {
	
	private static Logger log = Logger.getLogger(AceptarSolicitudesAction.class);
	
	public ActionForward executeWithAccess(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		
		UserAgentDetector uaDetector = new UserAgentDetector(request);
		
		String AccionError = "";
		log.info("AceptarSolicitudesAction: executeWithAccess");
		try {

			ListarSolicitudesForm listarSolicitudesForm= (ListarSolicitudesForm) form;
			if (listarSolicitudesForm == null) {
				listarSolicitudesForm = new ListarSolicitudesForm();
			}

			GestionSolicitudes gestionSolicitudes = new GestionSolicitudes();
			Integer anyoAcademico = listarSolicitudesForm.getAnyoAcademicoSeleccionado();
			log.info("ListarSolicitudesDisponiblesAction: El a�o academico seleccionado es: "
					+ anyoAcademico);

			List listaSolicitudes = gestionSolicitudes.encuentraTodasSolicitudesDisponiblesPorAnyoAcademico(anyoAcademico);

			request.setAttribute("listaSolicitudes", listaSolicitudes);
			return (mapping.findForward(uaDetector.composeForward("continue")));

		} catch (Exception e) {
			log.error("Error", e);
			request.setAttribute("excepcion", e);
			return (mapping.findForward(uaDetector.composeForward("error")));
		}
	}
    @Override
	public ActionForward setUpForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
    	UserAgentDetector uaDetector = new UserAgentDetector(request);
    	
    	log.info("ListarSolicitudesAction: setUpForm");
    	
		ListarSolicitudesForm listarSolicitudesForm = (ListarSolicitudesForm) form;
		if (listarSolicitudesForm == null) {
			listarSolicitudesForm = new ListarSolicitudesForm();
		}

		try {
			GestionSolicitudes gestionSolicitudes = new GestionSolicitudes();
			List anyosAcademicos = gestionSolicitudes.encuentraAnyosAcademicosDistintos();
			log.debug("losa a�os academicos encontrados son los siguientes: " + anyosAcademicos);
			listarSolicitudesForm.setListaAnyosAcademicos(anyosAcademicos);

			Integer anyoAcademico = listarSolicitudesForm.getAnyoAcademicoSeleccionado();
			log.info("ListarsDisponiblesAction: El a�o academico seleccionado es: "+ anyoAcademico);

			List<Solicitud> listaSolicitudes = null;
			if (anyoAcademico ==null)
			{
				listarSolicitudesForm.setAnyoAcademicoSeleccionado(0);
				log.debug("El a�o seleccionado es nulo con lo que buscamos todas las solicitudes");
				listaSolicitudes = gestionSolicitudes.encuentraTodasSolicitudes();
				
			}
			else {
				listarSolicitudesForm.setAnyoAcademicoSeleccionado(anyoAcademico);
				log.debug("Tenemos a�o seleccionado y por tanto buscamos las solicitudes de ese a�o");
				listaSolicitudes = gestionSolicitudes.encuentraTodasSolicitudesDisponiblesPorAnyoAcademico(anyoAcademico);
			}
			log.info("ListarSolicitudesAction: La lista de solicitudes es" + listaSolicitudes);
			request.setAttribute("listaSolicitudes", listaSolicitudes);
						
		} catch (ObjectNotFoundException e) {
			
		} catch (Exception e) {
			log.error("Error", e);
			request.setAttribute("excepcion", e);
			return (mapping.findForward(uaDetector.composeForward("error")));
		}
		
		return (mapping.findForward(uaDetector.composeForward("continue")));
	}
}
